/**
 * 自定义分页组件
 * 提供分页显示页码的功能，点击下一页自动显示到第几页（高亮），不提供数据渲染
 * 可以通过传入回调函数获取每次点击的页码，自己实现数据渲染功能
 * 依赖jquery
 * 引入即可使用
 * 使用办法：
 * &……&
 * 页面中引入jquery.js 和 本js
 * 然后在合适位置加入如下标签：
 * <page total="1000" pagesize="20" onPageChange="dosomething"></page>
 * 解释：
 * total：数据总数
 * pagesize：每页显示数
 * onPageChange:页面点击事件回调 如下：
 *  function dosomething(page){
 *      console.log("当前点击的页码数："+page);
 *  }
 *
 * @author：gaotao
 */
var pagination = pagination || {};
var _global_Pagination = {};
$(function () {
    function Pagination() {}
    Pagination.prototype = {
        render: function (obj) {
            this._wrap = obj.wrap;
            this._total = obj.total;
            this._pagesize = obj.pagesize;
            this._currentPage = obj.currentPage;
            this._cb = obj.onPagechange;
            if (obj.btnCount < 5) {
                obj.btnCount = 5;
            } else if (obj.btnCount % 2 == 0) {
                obj.btnCount = obj.btnCount + 1;
            }
            this._btnsValue = obj.btnCount ? obj.btnCount : 7;
            this._halfbtns = parseInt((this._btnsValue - 3) / 2);
            this._btnNum = obj.total / obj.pagesize > parseInt(obj.total / obj.pagesize) ? parseInt(obj.total / obj.pagesize) + 1 : parseInt(obj.total / obj.pagesize);
        }, bindEvent: function () {
            var that = this;
            that._wrap.on('click', '.pagenum', function () {
                that._currentPage = parseInt($(this).text());
                eval(that._cb+"("+that._currentPage+")");
                isshowMore.call(that);
            });
            that._wrap.on('click', '#pagination-prev', function () {
                if ($(this).hasClass('pagination-disabled')) {
                    return;
                }
                that._currentPage--;
                $('#pagination-next').hasClass('pagination-disabled') && $('#pagination-next').removeClass('pagination-disabled');
                if (that._currentPage == 1) {
                    $('#pagination-prev').addClass('pagination-disabled');
                } else {
                    $('#pagination-prev').removeClass('pagination-disabled');
                }
                eval(that._cb+"("+that._currentPage+")");
                isshowMore.call(that);
            });
            that._wrap.on('click', '#pagination-next', function () {
                if ($(this).hasClass('pagination-disabled')) {
                    return;
                }
                that._currentPage++;
                $('#pagination-prev').hasClass('pagination-disabled') && $('#pagination-prev').removeClass('pagination-disabled');
                if (that._currentPage == that._btnNum) {
                    $('#pagination-next').addClass('pagination-disabled');
                } else {
                    $('#pagination-next').removeClass('pagination-disabled')
                }
                eval(that._cb+"("+that._currentPage+")");
                isshowMore.call(that);
            });
            isshowMore.call(this);
            function isshowMore() {
                if (this._btnNum <= this._btnsValue) {
                    creatBtns.call(this, 'none')
                } else {
                    if (this._currentPage <= (this._btnsValue - 1 - this._halfbtns)) {
                        creatBtns.call(this, 'after');
                    } else if (this._currentPage >= this._btnNum - 1 - this._halfbtns) {
                        creatBtns.call(this, 'before')
                    } else {
                        creatBtns.call(this, 'all')
                    }
                }
            }

            function creatBtns(ismorePosition) {
                var html = '';
                var ismore = '<li class="pagination-ellipsis" style="color:#337ab7;position:relative;top:-5px;display:inline-block;padding:0 .12rem;margin-right:4px">...</li>';
                var firstbtn = '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num="1">1</li>';
                var lastbtn = '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num=' + this._btnNum + '>' + this._btnNum + '</li>';
                var prevbtn = '<span class="pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" id="pagination-prev"><</span>';
                var nextbtn = '<span class="pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" id="pagination-next">></span>'
                if (this._currentPage == 1) {
                    firstbtn = '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num="1">1</li>';
                    prevbtn = '<span class="pagination-btn pagination-disabled" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" id="pagination-prev"><</span>'
                }
                if (this._currentPage == this._btnNum) {
                    lastbtn = '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num=' + this._btnNum + '>' + this._btnNum + '</li>';
                    nextbtn = '<span class="pagination-btn pagination-disabled" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" id="pagination-next">></span>'
                }
                if (ismorePosition == 'none') {
                    for (var i = 1; i <= this._btnNum; i++) {
                        if (i == this._currentPage) {
                            html += '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num=' + i + '>' + i + '</li>';
                        } else {
                            html += '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num=' + i + '>' + i + '</li>';
                        }
                    }
                }
                if (ismorePosition == "after") {
                    for (var i = 1; i <= this._btnsValue - 1; i++) {
                        if (i == this._currentPage) {
                            html += '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num=' + i + '>' + i + '</li>';
                        } else {
                            html += '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num=' + i + '>' + i + '</li>';
                        }
                    }
                    html = html + ismore + lastbtn;
                }
                if (ismorePosition == "before") {
                    html = html + firstbtn + ismore;
                    for (var i = this._btnNum - (this._btnsValue - 2); i <= this._btnNum; i++) {
                        if (i == this._currentPage) {
                            html += '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num=' + i + '>' + i + '</li>';
                        } else {
                            html += '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num=' + i + '>' + i + '</li>';
                        }
                    }
                }
                if (ismorePosition == "all") {
                    var halfnum = parseInt((this._btnsValue - 3) / 2);
                    html += firstbtn + ismore;
                    for (var i = (this._currentPage - halfnum); i <= this._currentPage + halfnum + ((this._btnsValue - 3) % 2); i++) {
                        if (i == this._currentPage) {
                            html += '<li class="pagenum pagination-btn pagination-current" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center;border:1px solid #3e8ddd;color:white;background:#3e8ddd;" data-num=' + i + '>' + i + '</li>'
                        } else {
                            html += '<li class="pagenum pagination-btn" style="padding:.06rem .1rem;margin-right:4px;border-radius:4px;border:1px solid #ccc;cursor:pointer;color:#337ab7;display:inline-block;box-sizing:border-box;width:44px;text-align:center" data-num=' + i + '>' + i + '</li>'
                        }
                    }
                    html += ismore + lastbtn;
                }
                that._wrap.html(prevbtn + '<ul class="pagination-wrap" style=" position:relative;display:inline-block;padding-left:0">' + html + '</ul>' + nextbtn);
            }
        }, init: function (paginationObj) {
            this.render(paginationObj);
            this.bindEvent();
        }
    }
    pagination.init = function () {
        var $page = $("page");
        $page.unbind("click");//先清空当前节点下所有点击事件
        var total = $page.attr("total");
        var pagesize = $page.attr("pagesize");
        var onPageChange = $page.attr("onPageChange");
        if(onPageChange&&onPageChange.indexOf("(")!=-1){
            onPageChange = onPageChange.substring(0,onPageChange.indexOf("("));
        }
        var obj = {
            wrap:$page, //页面显示分页器容器对象
            total:total,//总条数
            pagesize:pagesize,//每页显示条
            currentPage:1//当前页
            ,onPagechange:onPageChange
            //,btnCount:7 页数过多时，显示省略号的边界页码按钮数量，可省略，且值是大于5的奇数
        }

        $(".pagenum").unbind();
        $("#pagination-prev").unbind();
        $("#pagination-next").unbind();
        _global_Pagination = new Pagination();
        return _global_Pagination.init(obj)
    }
    setInterval(function(){
        var pagenum = $("page ul li:last").text();
        var pagesize = $("page").attr("pagesize");
        var total = $("page").attr("total");
        if(pagenum!=Math.ceil(total/pagesize)) {
            pagination.init();
        }
    },500);
});

